<template>
  <div>
    <el-dialog 
      width="1200px"
      :title="$t('商户管理列表')"
      :visible.sync="isShow"
      :close-on-click-modal="false"
    >
      <div class="head_search u-fw">
        <div class="ipt_box">
          <p class="label font14 text_black">{{$t('管理员名称')}}：</p>
          <el-input
            v-model="manage"
            :placeholder="$t('请输入管理员名称')"
          ></el-input>
        </div>
        <div class="ipt_box" style="width: 260px;">
          <p class="label font14 text_black">{{$t('手机号')}}：</p>
          <el-input v-model="phone" :placeholder="$t('请输入手机号')"></el-input>
        </div>
        
        <div class="div_box u-f u-f-ac">
        <p class="label font14 text_black">{{$t('所属代理商')}}：</p>
        <div
          class="content font14 text_black u-f u-f-ac"
          @click="openAgentList"
        >
          <p class="text-ellipsis">{{ agent ? agent : $t('选择代理商') }}</p>
        </div>
        <button class="btn font14 text_white bg_danger" @click="clearAgent">
          {{$t('清除')}}
        </button>
      </div>
        <div class="select_box">
          <p class="label font14 text_black">{{$t('所属业务员')}}：</p>
          <el-select
            v-model="salesmanVal"
            :placeholder="$t('请选择')"
            @change="salesmanChange"
          >
            <el-option
              v-for="item in salesmanArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
        <div class="select_box" style="width: 260px;">
          <p class="label font14 text_black">{{$t('状态')}}：</p>
          <el-select
            v-model="manageStatusVal"
            :placeholder="$t('请选择')"
            @change="changeManageStatus"
          >
            <el-option
              v-for="item in manageStatusArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </div>
        <button class="sub_btn font14 text_white bg_blue" @click="getData('search')">
          {{$t('搜索')}}
        </button>
      </div>

      <div class="table_box">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="name" :label="$t('管理员名称')"></el-table-column>
          <el-table-column
            prop="phone"
            :label="$t('手机号')"
            width="110"
          ></el-table-column>
          <el-table-column
            prop="agency_name"
            :label="$t('所属代理')"
          ></el-table-column>
          <el-table-column
            prop="brokerage"
            :label="$t('佣金比例')"
            width="100"
          >
            <template slot-scope="{ row }">
              <span>{{row.brokerage}}%</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            :label="$t('用户状态')"
            width="100"
          ></el-table-column>
          <el-table-column
            prop="open_lock"
            :label="$t('是否可开锁')"
            width="120"
          >
            <template slot-scope="{ row }">
              <span v-if="row.open_lock == 1">{{$t('是')}}</span>
              <span v-if="row.open_lock == 0">{{$t('否')}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="employee_name"
            :label="$t('所属业务员')"
            width="120"
          ></el-table-column>
          <el-table-column :label="$t('操作')" width="100">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="handleChange(scope.row)"
                >{{$t('选择')}}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination_box ">
          <el-pagination
            background
            
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-size="page_size"
            layout="total, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </el-dialog>

    <agent-list
      ref="agentList"
      :memusid="memusid"
      :uid="uid"
      @hide="closeAgentList"
    ></agent-list>
  </div>
  
</template>

<script>
import agentList from "components/agent-list/agent-list.vue";
import util from '@/assets/js/util.js'
export default {
  name: "manageList",
  data() {
    let that = this
    return {
      page: 1,
      page_size: 20,
      total: 0,
      isShow: false,
      manage: "",
      phone: "",
      manageStatusArr: [
        {
          value: 0,
          label: that.$t('选择管理员状态')
        },
        {
          value: 1,
          label: that.$t('正常')
        },
        {
          value: 2,
          label: that.$t('禁用')
        }
      ],
      manageStatusVal: 0,
      salesmanVal: 0,
      salesmanArr: [
        {
          id: 0,
          name: that.$t('选择业务员')
        },
      ],
      tableData: [],
      agent: "",
      agent_id: "",
    };
  },
  props: {
    type: {
      type: [Number, String],
      default: 1
    },
    memusid:{
      type:[Number, String],
      default: 1
    },
    uid:{
      type:[Number, String],
      default: 1
    }

  },
  components: {
    agentList,
  },
  methods: {
    show() {
      this.manage = ''
      this.phone = ''
      this.manageStatusVal = 0
      this.salesmanVal = 0
      this.agent = ''
      this.agent_id = ''
      this.page = 1
      this.page_size = 20
      this.isShow = true;

      setTimeout(() => {
        if(this.memusid==5){
          this.agent_id = this.uid;
          this.agent = this.name;

          this.getData();
        }else{
          this.getData();
        }
        
      }, 150);
    },
    getData(type) {
      if(type == 'search') { this.page = 1 }
      util.$http('/agency/sellerDialog?page=' + this.page, {
        page_size: this.page_size,
        name: this.manage,
        phone: this.phone,
        agency_id: this.agent_id,
        agency_name: this.agent,
        employee_id: this.salesmanVal,
        status: this.manageStatusVal,
      })
      .then(res=>{
        if(res.code == 1) {
          this.total = res.data.total
          this.tableData = res.data.list
        }
      })
    },
    handleCurrentChange(val) {
      this.page = Number(val)
      this.getData()
    },
    changeManageStatus(val) {
      this.manageStatusVal = val;
    },
    salesmanChange(val) {
      this.salesmanVal = val;
    },
    openAgentList() {
      this.$refs.agentList.show();
    },
    closeAgentList(data) {
      let employArr = [
        {
          id: 0,
          name: this.$t('选择业务员')
        }
      ]
      this.agent = data.name
      this.agent_id = data.id
      this.getEmployee(data.id,res=>{
        this.salesmanArr = [...employArr, ...res]
        this.salesmanVal = 0
      })
    },
    getEmployee(id, callback) {
      util.$http('/seller/employeeList', {
        id
      })
      .then(res=>{
        if(res.code == 1) {
          callback(res.data)
        }
      })
    },
    clearAgent() {
      this.agent = ""
      this.agent_id = ""
      this.salesmanArr = [
        {
          id: 0,
          name: this.$t('选择业务员')
        }
      ]
      this.salesmanVal = 0
    },
    handleChange(row) {
      this.isShow = false;
      this.$emit('hide', row, this.type)
    }
  }
};
</script>

<style scoped>
.head {
  padding: 20px 20px 30px;
}
.head .icon_box {
  display: inline-block;
  width: 30px;
  height: 30px;
  cursor: pointer;
}
.head_search {
  display: flex;
  align-items: center;
  padding: 0 20px;
  margin-bottom: 20px;
}
.head_search .ipt_box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 290px;
  margin-right: 20px;
}
.head_search .ipt_box .el-input {
  width: 200px;
}
.head_search .select_box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 290px;
  margin-right: 20px;
}
.head_search .select_box .el-select {
  width: 200px;
}
.head_search .div_box {
  width: 320px;
  margin-right: 20px;
}
.head_search .div_box .content {
  width: 170px;
  height: 40px;
  padding-left: 15px;
  border: 1px solid #dcdfe6;
  border-radius: 4px 0 0 4px;
  transition: border 0.3s;
  box-sizing: border-box;
}
.head_search .div_box .content:active {
  border-color: #409eff;
}
.head_search .div_box .btn {
  width: 60px;
  height: 40px;
  border: 0;
  outline: 0;
  cursor: pointer;
  border-radius: 0 4px 4px 0;
  box-sizing: border-box;
}
.head_search .sub_btn {
  width: 70px;
  height: 40px;
  border: 0;
  outline: 0;
  cursor: pointer;
  border-radius: 4px;
}
.table_box {
  padding: 0 20px;
}
.pagination_box {
  padding: 30px 0;
}
.pagination_box .el-pagination {
  display: flex;
  justify-content: flex-end;
}
</style>
